<!--
*Author：富行天下团队

 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *业务请在@/extension/mall/MebCoupon.js此处编写
 -->
<template>
  <div>
    <!-- 制券 -->
    <VolBox :model.sync="model" title="制券" :width="600" :height="400">
      <VolForm
        ref="myform"
        :label-width="100"
        :loadKey="true"
        :formFields="editFormFields"
        :formRules="editFormOptions"
      ></VolForm>
      <div slot="footer">
        <Button type="default" @click="submit" :loading="loading">确认</Button>
      </div>
    </VolBox>
    <VolBox :model.sync="modelXQ" title="制券详情" :width="600" :height="400">
      <VolForm
        ref="myformXQ"
        :label-width="100"
        :loadKey="true"
        :formFields="editFormFieldsXQ"
        :formRules="editFormOptionsXQ"
      ></VolForm>
      <div slot="footer">
        <Button @click="modelXQ = false">关闭</Button>
      </div>
    </VolBox>
    <!-- 查看券 -->
    <!-- <VolBox :model.sync="model2" title="查看券" :width="1000" :height="400">
      <VolTable
        ref="editTable1"
        :columns="columns"
        :max-height="270"
        :index="true"
        :ck="false"
        :tableData="tableData"
      >
      </VolTable>
    </VolBox> -->
    <!-- 查看券模版 -->
    <VolBox :model.sync="model2" title="查看券模版" :width="1000" :height="550">
      <div class="mebcouponZQ">
        <VolForm
          ref="myform"
          :label-width="100"
          :loadKey="true"
          :formFields="formFields1"
          :formRules="formRules1"
        ></VolForm>
      </div>

      <el-form
        ref="editFormFields1"
        :rules="rules"
        :model="editFormFields1"
        :inline="true"
        label-width="100px"
      >
        <el-form-item label="券模板名称" prop="name">
          <el-input
            v-model="editFormFields1.name"
            placeholder="请输入券模板名称"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <el-form-item label="券类型" prop="type">
          <el-select
            v-model="editFormFields1.type"
            placeholder="请输入券类型"
            :disabled="true"
          >
            <el-option
              v-for="(i, ii) in typeList"
              :key="ii"
              :label="i.label"
              :value="i.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="券模板编码" required>
          <el-input
            v-model="editFormFields1.discountCode"
            :disabled="true"
            placeholder="请输入券模板编码"
          ></el-input>
        </el-form-item>
        <el-form-item label="券模板描述">
          <el-input
            v-model="editFormFields1.discountRe"
            :disabled="true"
            placeholder="请输入券模板描述"
          ></el-input>
        </el-form-item>
        <el-form-item v-show="coupValuStatus" label="面额" prop="coupon_value">
          <el-input
            class="meInt"
            v-model="editFormFields1.coupon_value"
            placeholder="请输入面额"
            :disabled="true"
          >
          </el-input>
          <div class="fr">
            <span v-if="couponStatus">元</span>
            <span v-else>折</span>
          </div>
        </el-form-item>
        <el-form-item class="sysm" label="使用说明" prop="note">
          <el-input
            :autosize="{ minRows: 4 }"
            v-model="editFormFields1.note"
            type="textarea"
            placeholder="请输入使用说明"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <div class="hx">财务规则</div>
        <el-form-item label="是否入账" prop="is_entry_accoun">
          <el-switch
            v-model="editFormFields1.is_entry_accoun"
            active-value="1"
            inactive-value="0"
            :disabled="true"
          >
          </el-switch>
        </el-form-item>
        <div class="hx">使用规则</div>
        <el-form-item label="使用门槛" prop="use_threshold">
          <el-radio
            v-model="editFormFields1.use_threshold"
            label="0"
            :disabled="true"
            >无门槛</el-radio
          >
          <el-radio
            v-model="editFormFields1.use_threshold"
            label="1"
            :disabled="true"
            >有门槛</el-radio
          >
          <div v-show="thresholdValue">
            满
            <el-input
              size="mini"
              class="z"
              v-model="editFormFields1.threshold_value"
              :disabled="true"
            />元可用
          </div>
        </el-form-item>
        <el-form-item label="是否互斥" prop="activity_mutex_type">
          <el-switch
            v-model="editFormFields1.activity_mutex_type"
            active-value="1"
            inactive-value="0"
            :disabled="true"
          ></el-switch>
        </el-form-item>
        <el-form-item label="优惠方案" prop="discount_id">
          <el-select
            v-model="editFormFields1.discount_id"
            placeholder="请输入优惠方案"
            :disabled="true"
          >
            <el-option
              v-for="(i, ii) in discount_List"
              :key="ii"
              :label="i.label"
              :value="i.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="sysm" label="规则说明" prop="regulation">
          <el-input
            :autosize="{ minRows: 4 }"
            v-model="editFormFields1.regulation"
            type="textarea"
            placeholder="请输入规则说明"
            :disabled="true"
          ></el-input>
        </el-form-item>
        <div class="hx">适用范围</div>
        <div>
          <el-radio-group
            v-model="editFormFields1.applicability"
            prop="applicability"
            :disabled="true"
          >
            <el-radio label="1" :disabled="applicabilityDisabled"
              >通用</el-radio
            >
            <el-radio label="2" :disabled="applicabilityDisabled"
              >指定品类</el-radio
            >
            <el-radio label="3">指定商品</el-radio>
            <el-radio label="4" :disabled="applicabilityDisabled"
              >指定供应商
            </el-radio>
          </el-radio-group>
          <!-- 指定品类 -->
          <div class="cascader" v-if="editFormFields1.applicability == 2">
            <span v-for="(i, ii) in PLProtyLIST" :key="ii"
              >{{ ii + 1 }}.{{ i.product_category_name }},</span
            >
          </div>
          <!-- 指定商品 -->
          <div class="cascader" v-else-if="editFormFields1.applicability == 3">
            <span v-for="(i, ii) in PLProtyLIST" :key="ii"
              >{{ ii + 1 }}.{{ i.product_category_name }},</span
            >
            <!-- <div class="casd">
                <el-cascader
                  ref="cascder"
                  v-model="cascaderValue"
                  :options="PLProtyLIST"
                  clearable
                  :props="{
                    checkStrictly: true,
                    label: 'typeName',
                    value: 'id'
                  }"
                ></el-cascader>
              </div>
              <el-select
                v-if="applicabilityDisabled == true"
                v-model="selectValue"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in productsList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.id"
                >
                </el-option>
              </el-select>
              <el-transfer
                v-else
                v-model="selectValue"
                :data="productsList"
                :titles="titles"
                :right-default-checked="selectValue"
              ></el-transfer> -->
          </div>
          <!-- 指定商户 -->
          <div class="cascader" v-else-if="editFormFields1.applicability == 4">
            <span v-for="(i, ii) in PLProtyLIST" :key="ii"
              >{{ ii + 1 }}.{{ i.supplierName }},<span
                v-if="i.mall_supplier_points.length > 0"
              >
                <!-- {{i.mall_supplier_points}} -->
              </span></span
            >
          </div>
        </div>
        <div class="hx">领取规则</div>
        <el-form-item label="是否领取" prop="receive_way">
          <el-switch
            v-model="editFormFields1.receive_way"
            active-value="1"
            inactive-value="0"
            :disabled="true"
          >
          </el-switch>
        </el-form-item>
        <div v-show="receiveWay">
          <el-form-item label="适用人群" prop="applicable_people">
            <el-radio-group
              v-model="editFormFields1.applicable_people"
              prop="applicable_people"
              :disabled="true"
            >
              <el-radio label="1">全部</el-radio>
              <el-radio label="2">特定人群</el-radio>
              <el-radio label="3">指定人群</el-radio>
            </el-radio-group>
          </el-form-item>
          <!-- 特定人群 -->
          <el-form-item v-show="rqStatus">
            消费未满
            <el-input
              size="mini"
              class="z"
              v-model="editFormFields1.amount_condition"
              :disabled="true"
            />元可领
          </el-form-item>
          <!-- 指定人群 -->
          <div class="cascader" v-show="zdPeo">
            <!-- <el-transfer
                v-model="memberType"
                :data="hylxList"
                :titles="titles3"
                :right-default-checked="memberType"
              ></el-transfer> -->
          </div>
          <el-form-item label="领取时间">
            <span>{{ editFormFields1.receiveTime1 }}</span> 至<span>{{
              editFormFields1.receiveTime2
            }}</span>
          </el-form-item>
          <el-form-item label="领取限制">
            <span>每人每天可领取</span>
            <el-input
              class="z"
              size="mini"
              v-model="editFormFields1.per_daylimit"
              :disabled="true"
            ></el-input>
            <span>张，每人总计可领取</span>
            <el-input
              class="z"
              size="mini"
              v-model="editFormFields1.per_limit"
              :disabled="true"
            ></el-input>
            <span>张</span>
          </el-form-item>
        </div>
      </el-form>
    </VolBox>

    <VolBox :model.sync="qrmodel" title="二维码" :width="1000" :height="400">
      <div>
        <el-input size="small" style="width: 200px" v-model="search" />
        <Button type="success" @click="cx">查询</Button>
        <Button type="info" @click="down">下载二维码</Button>
      </div>
      <VolTable
        ref="editTable12"
        :columns="qrColumns"
        :max-height="350"
        :index="true"
        :tableData="QRTableData"
      >
      </VolTable>
      <div slot="footer">
        <Button type="default" @click="submit" :loading="loading">确认</Button>
      </div>
    </VolBox>
  </div>
</template>

<script>
import VolBox from "@/components/basic/VolBox.vue";
import VolForm from "@/components/basic/VolForm.vue";
import VolTable from "@/components/basic/VolTable.vue";
import JSZip from "jszip";
import FileSaver from "file-saver";
import axios from "axios";
var vueParam = {
  components: {
    VolBox,
    VolForm,
    VolTable
  },
  data() {
    return {
      defaultProps: {
        id: "id",
        children: "children",
        label: "label"
      },
      //穿梭框选中数据
      treeChecked: [],
      // 穿梭框数据
      treeTransferData: [],
      // model: false,
      title: "",
      rowAll: {},
      formFields1: { coupon_cover: "" },
      formRules1: [
        [
          {
            field: "coupon_cover",
            title: "券面图",
            type: "img",
            maxSize: 1,
            maxFile: 1,
            multiple: true,
            storage: "OSS_Y",
            url: "/api/MebCoupon/Upload",
            disabled: true
          }
        ]
      ],
      editFormFields1: {
        discount_id: "",
        discountCode: "",
        discountRe: "",
        is_entry_accoun: "1",
        creator: "",
        createDate: "",
        name: "",
        type: "",
        note: "",
        coupon_value: 0,
        count: "",
        use_threshold: "0",
        activity_mutex_type: "",
        regulation: "",
        applicability: 1,
        receive_way: "",
        applicable_people: "",
        receiveTime1: "",
        receiveTime2: "",
        useTime: "",
        per_daylimit: "",
        per_limit: "",
        member_type: "",
        member_level: "",
        threshold_value: "",
        amount_condition: 0
      },
      memberType: [],
      rules: {
        name: [
          { required: true, message: "请输入券模板名称", trigger: "blur" }
        ],
        type: [{ required: true, message: "请输入券类型", trigger: "change" }]
      },
      typeList: [],
      rqStatus: false,
      hylxList: [],
      hydjList: [],
      thresholdValue: false,
      defaultCheckedKeys: [],
      cascaderValue: [],
      selectValue: [],
      productsList: [],
      titles: ["待选商品", "已选商品"],
      titles2: ["待选品类", "已选品类"],
      titles3: ["待选类型", "已选类型"],
      titles4: ["待选商户", "已选商户"],
      transferValue: [],
      transferProtype: [],
      treeProtyLIST: [],
      PLProtyLIST: [],
      receiveWay: false,
      couponStatus: true,
      coupValuStatus: false,
      zdPeo: false,
      zdPeoValue: [],
      zdPeoProductList: [],
      member_level: [],
      MerchantLIST: [],
      MerchantValue: [],
      arrMax: [],
      applicabilityDisabled: false,
      model: false,
      editFormFields: {
        coupon_name: "",
        time_type: 0,
        card_num: "",
        cardno_gdrq: "",
        cardno_suffix: "",
        random_type: "",
        rule_type: 0,
        cardno_prefix: "",
        start_no: "",
        is_generate: 1,
        coupon_code: "",
        coupon_count: "",
        creator: "",
        createDate: "",
        coupon_note: "",
        card_day: ""
      },
      model2: false,
      editFormOptions: [
        [
          {
            title: "优惠券模板名称",
            field: "coupon_code",
            type: "select",
            dataKey: "coupontype",
            data: [],
            required: true
          }
        ],
        [
          {
            title: "券名称",
            field: "coupon_name",
            type: "text",
            required: true
          },
          {
            title: "券号位数",
            field: "card_num",
            type: "number",
            required: true,
            max: 15,
            min: 6
          }
        ],
        [
          {
            title: "是否生成券",
            field: "is_generate",
            type: "switch",
            // onChange: (val, option) => {
            //   let coupon_count = this.getFormOption("coupon_count");
            //   if (val == 0) {
            //     coupon_count.hidden = true;
            //     coupon_count.required = false;
            //   } else {
            //     coupon_count.hidden = false;
            //     coupon_count.required = true;
            //   }
            // }
          },
          {
            title: "发劵数量",
            field: "coupon_count",
            type: "number",
            max: 10000,
            required: true
          }
        ],
        [
          {
            title: "劵号规则",
            field: "rule_type",
            type: "radio",
            data: [
              { key: 0, value: "自定义模式" },
              { key: 1, value: "随机模式" }
            ]
          },
          {
            title: "劵前缀",
            field: "cardno_prefix",
            type: "text",
            hidden: false,
            required: true
          }
        ],
        [
          {
            title: "劵起始号",
            field: "start_no",
            type: "number",
            hidden: false,
            required: true
          },
          {
            title: "劵后缀",
            field: "cardno_suffix",
            type: "text",
            hidden: false,
            required: false
          }
        ],
        [
          {
            title: "随机模式",
            field: "random_type",
            type: "select",
            dataKey: "MebCardRules",
            data: [],
            hidden: true,
            required: true
          }
        ],
        [
          {
            title: "有效期规则",
            field: "time_type",
            type: "radio",
            data: [
              { key: 0, value: "固定区间模式" },
              { key: 1, value: "随机模式" }
            ]
          },
          {
            title: "区间",
            field: "cardno_gdrq",
            type: "datetime",
            range: true
          }
        ],
        [
          {
            title: "描述",
            field: "coupon_note",
            type: "textarea",
            colSize: 12
          }
        ]
      ],
      model2: false,
      columns: [
        {
          field: "coupon_batch",
          title: "批号",
          type: "int",
          width: 120,
          align: "left",
          sortable: true
        },
        {
          field: "coupon_name",
          title: "券名称",
          type: "string",
          width: 150,
          align: "left"
        },
        {
          field: "status",
          title: "状态",
          type: "int",
          bind: { key: "", data: [] },
          width: 120,
          align: "left"
        },
        {
          field: "coupon_count",
          title: "数量",
          type: "int",
          width: 120,
          align: "left"
        },
        {
          field: "coupon_note",
          title: "描述",
          type: "string",
          width: 120,
          align: "left"
        },
        {
          field: "creator",
          title: "创建人",
          type: "string",
          width: 120,
          align: "left"
        },
        {
          field: "createDate",
          title: "创建时间",
          type: "datetime",
          width: 120,
          align: "left",
          sortable: true
        },
        {
          field: "editor",
          title: "修改人",
          type: "string",
          width: 120,
          align: "left"
        },
        {
          field: "edittime",
          title: "修改时间",
          type: "datetime",
          width: 120,
          align: "left",
          sortable: true
        }
      ],
      tableData: [],
      couponZQ: {},
      loading: false,
      modelXQ: false,
      editFormFieldsXQ: {
        coupon_name: "",
        time_type: 0,
        card_num: "",
        cardno_gdrq: "",
        cardno_suffix: "",
        random_type: "",
        rule_type: 0,
        cardno_prefix: "",
        start_no: "",
        is_generate: 1,
        coupon_code: "",
        coupon_count: "",
        creator: "",
        createDate: "",
        coupon_note: "",
        card_day: ""
      },
      editFormOptionsXQ: [
        [
          {
            title: "优惠券模板",
            field: "coupon_code",
            type: "select",
            dataKey: "coupontype",
            data: [],
            disabled: true
          }
        ],
        [
          {
            title: "券名称",
            field: "coupon_name",
            type: "text",
            disabled: true
          },
          {
            title: "券号位数",
            field: "card_num",
            type: "number",
            disabled: true
          }
        ],
        [
          {
            title: "是否生成券",
            field: "is_generate",
            type: "switch",
            disabled: true
          },
          {
            title: "发劵数量",
            field: "coupon_count",
            type: "number",
            max: 10000,
            disabled: true
          }
        ],
        [
          {
            title: "劵号规则",
            field: "rule_type",
            type: "radio",
            data: [
              { key: 0, value: "自定义模式" },
              { key: 1, value: "随机模式" }
            ],
            disabled: true
          },
          {
            title: "劵前缀",
            field: "cardno_prefix",
            type: "text",
            hidden: false,
            disabled: true
          }
        ],
        [
          {
            title: "劵起始号",
            field: "start_no",
            type: "number",
            hidden: false,
            disabled: true
          },
          {
            title: "劵后缀",
            field: "cardno_suffix",
            type: "text",
            hidden: false,
            disabled: true
          }
        ],
        [
          {
            title: "随机模式",
            field: "random_type",
            type: "select",
            dataKey: "MebCardRules",
            data: [],
            hidden: true,
            disabled: true
          }
        ],
        [
          {
            title: "有效期规则",
            field: "time_type",
            type: "radio",
            data: [
              { key: 0, value: "固定区间模式" },
              { key: 1, value: "随机模式" }
            ],
            disabled: true
          },
          {
            title: "区间",
            field: "cardno_gdrq",
            type: "datetime",
            range: true,
            disabled: true
          }
        ],
        [
          {
            title: "描述",
            field: "coupon_note",
            type: "textarea",
            colSize: 12,
            disabled: true
          }
        ]
      ],
      discount_List: [],
      qrmodel: false,
      qrColumns: [
        {
          field: "coupon_make_name",
          title: "制券名称",
          type: "int",
          width: 180,
          align: "left"
        },
        {
          field: "couponid",
          title: "优惠券id",
          type: "int",
          width: 210,
          align: "left",
          sortable: true
        },
        {
          field: "imgpic",
          title: "二维码",
          type: "img",
          width: 210,
          align: "left",
          sortable: true
        },
        {
          field: "memberid",
          title: "手机号",
          type: "int",
          width: 120,
          align: "left"
        },
        {
          field: "member_name",
          title: "领取人名称",
          type: "string",
          width: 90,
          align: "left"
        },
        {
          field: "orderid",
          title: "订单id",
          type: "string",
          width: 90,
          align: "left"
        },
        {
          field: "ordersn",
          title: "订单码",
          type: "string",
          width: 90,
          align: "left"
        }
      ],
      QRTableData: [],
      search: "",
      searchRow: ""
    };
  },
  watch: {
    "editFormFields.rule_type"(v, n) {
      let cardno_prefix = this.getFormOption("cardno_prefix");
      let start_no = this.getFormOption("start_no");
      let cardno_suffix = this.getFormOption("cardno_suffix");
      let random_type = this.getFormOption("random_type");

      console.log(v, "v");
      if (v == 0) {
        cardno_prefix.hidden = false;
        cardno_prefix.required = true;
        random_type.required = false;
        start_no.hidden = false;
        cardno_suffix.hidden = false;
        cardno_suffix.required = true;
        random_type.hidden = true;
      } else {
        cardno_suffix.required = false;
        cardno_prefix.required = false;
        cardno_prefix.required = false;
        cardno_suffix.required = false;
        cardno_prefix.hidden = true;
        start_no.hidden = true;
        cardno_suffix.hidden = true;
        random_type.hidden = false;
        random_type.required = true;
      }
    },
    "editFormFields.time_type"(v, n) {
      let time_type = this.getFormOption("time_type");
      if (v == 0) {
        this.editFormOptions[6][1] = {
          title: "区间",
          field: "cardno_gdrq",
          type: "datetime",
          range: true
        };
      } else {
        this.editFormOptions[6][1] = {
          title: "",
          field: "card_day",
          type: "textSpan",
          cont: ["发券生效后", "天有效"]
        };
      }
    },
    "editFormFields.card_day"(v, n) {
      let reg = /^[0-9]*$/; // 数字
      let reg2 = /^\+?[1-9][0-9]*$/; // 大于0整数
      if (!reg.test(v)) {
        this.$Message.error("请输入数字");
      } else {
        if (!reg2.test(v)) {
          this.$Message.error("请输入大于0整数");
        }
      }
    },
    "editFormFields.cardno_prefix"(v, n) {
      // console.log(typeof(v),"v")
      let calletter = /^[A-Za-z0-9]*$/g;
      if (!calletter.test(v)) {
        this.$Message.error("请输入大小写字母或者数字作为券前缀");
      }
    },
    "editFormFields.cardno_suffix"(v, n) {
      let calletter = /^[a-zA-z]*$/g;
      if (!calletter.test(v)) {
        this.$Message.error("请输入大写或者小写字母作为券后缀");
      }
    },
    "editFormFields.is_generate"(v, n) {
      let coupon_count = this.getFormOption("coupon_count");
      let rule_type = this.getFormOption("rule_type");
      if (v == 1) {
        coupon_count.hidden = false;
        coupon_count.required = true;
        rule_type.disabled = false;
      } else {
        coupon_count.required = false;
        coupon_count.hidden = true;
        this.editFormFields.rule_type = 1;
        rule_type.disabled = true;
      }
    },
  },
  methods: {
    openQR(e) {
      this.searchRow = e;
      let params = {
        order: "desc",
        page: 1,
        rows: 100,
        sort: "id",
        wheres: `[{\"name\":\"coupon_makeid\",\"value\":\"${e.coupon_makeid}\",\"displayType\":\"like\"},{\"name\":\"couponid\",\"value\":\"${this.search}\",\"displayType\":\"like\"}]`
      };
      this.http
        .post("/api/MebCouponhis/GetPageData_Allow", params)
        .then(res => {
          this.QRTableData = res.rows;
          this.qrmodel = true;
        });
    },
    cx() {
      this.openQR(this.searchRow);
    },
    down() {
      let srcList = this.$refs.editTable12.$refs.table.selection;
      if (srcList.length) {
        let ids = srcList
          .map(i => {
            return i.id;
          })
          .join();
        let params = { couponids: ids };
        this.http.post("/api/MebCouponhis/SaveCodePic", params).then(res => {
          this.openQR(this.searchRow);
          let name = srcList[0].coupon_make_name + this.formatLongDate();
          const zip = new JSZip(); // 创建实例对象
          const promises = [];
          // 遍历生成下载文件
          res.data.forEach((item, index) => {
            const promise = this.getFile(item).then(res => {
              const fvName = `${index + 1}.jpg`;
              zip.file(fvName, res.data, {
                binary: true
              });
            });
            promises.push(promise);
          });
          // 生成 zip 文件
          Promise.all(promises).then(() => {
            zip
              .generateAsync({
                type: "blob", // 文件格式
                compression: "DEFLATE", // STORE: 默认不压缩， DEFLATE：需要压缩
                compressionOptions: {
                  level: 9 // 压缩等级 1~9   1 压缩速度最快， 9 最优压缩方式
                }
              })
              .then(res => {
                FileSaver.saveAs(res, `${name}.zip`); // 使用FileSaver.saveAs保存文件，文件名可自定义
              });
          });
        });
      } else {
        this.$message.error("请选择数据");
      }
    },
    getFile(fvUrl) {
      return new Promise((resolve, reject) => {
        axios(fvUrl, {
          method: "get", // get请求
          responseType: "blob" // 返回的数据会被强制转为blob类型 ，转换成arraybuffer 也行
        })
          .then(res => {
            resolve(res); // 将下载的文件返回
          })
          .catch(err => {
            reject(err);
          });
      });
    },
    openUHFA(e) {
      let params = {
        order: "desc",
        page: 1,
        rows: 100,
        sort: "id",
        wheres: `[{\"name\":\"spare2\",\"value\":\"${e}\",\"displayType\":\"like\"}]`
      };
      this.http
        .post("/api/Mall_preferential_scheme/getPageDataPre", params)
        .then(res => {
          console.log(res, "v");
          if (res.rows.length > 0) {
            this.discount_List = [];
            res.rows.forEach(i => {
              let obj = {
                label: i.name,
                value: i.id,
                key: i.id
              };
              this.discount_List.push(obj);
            });
          } else {
            this.discount_List = [];
          }
        });
    },
    openXQ(e) {
      this.modelXQ = true;
      if (e.time_type == 0) {
        this.editFormOptionsXQ[6][1] = {
          title: "区间",
          field: "cardno_gdrq",
          type: "datetime",
          range: true
        };
      } else {
        this.editFormOptionsXQ[6][1] = {
          title: "",
          field: "card_day",
          type: "textSpan",
          cont: ["发券生效后", "天有效"]
        };
      }
      let cardno_prefix = this.getFormOptionXQ("cardno_prefix");
      let start_no = this.getFormOptionXQ("start_no");
      let cardno_suffix = this.getFormOptionXQ("cardno_suffix");
      let random_type = this.getFormOptionXQ("random_type");

      if (e.rule_type == 0) {
        cardno_prefix.hidden = false;
        start_no.hidden = false;
        cardno_suffix.hidden = false;
        random_type.hidden = true;
      } else {
        cardno_prefix.hidden = true;
        start_no.hidden = true;
        cardno_suffix.hidden = true;
        random_type.hidden = false;
      }
      this.editFormFieldsXQ.coupon_code = e.coupon_code;
      this.editFormFieldsXQ.coupon_name = e.coupon_name;
      this.editFormFieldsXQ.card_num = e.card_num;
      this.editFormFieldsXQ.cardno_suffix = e.cardno_suffix;
      this.editFormFieldsXQ.random_type = e.random_type;
      this.editFormFieldsXQ.rule_type = e.rule_type;
      this.editFormFieldsXQ.cardno_prefix = e.cardno_prefix;
      this.editFormFieldsXQ.is_generate = e.is_generate;
      this.editFormFieldsXQ.start_no = e.start_no;
      this.editFormFieldsXQ.coupon_count = e.coupon_count;
      this.editFormFieldsXQ.coupon_note = e.coupon_note;
      this.editFormFieldsXQ.card_day = e.card_day;
      this.editFormFieldsXQ.cardno_gdrq = [e.use_endtime, e.use_starttime];
    },

    GetVueDictionary() {
      let that = this;
      this.transferValue = [];
      this.typeList = [];
      console.log(this.typeList, "this.typeList");
      this.hylxList = [];
      this.productsList = [];
      this.selectValue = [];
      let param = ["Coupon_Type", "Member_Type", "supplier"];
      this.http.post("/api/Dictionary/GetVueDictionary", param).then(res => {
        res.forEach(o => {
          if (o.dicNo == "Coupon_Type") {
            o.data.forEach(i => {
              let obj = {
                label: i.value,
                value: i.key
              };
              this.typeList.push(obj);
            });
          } else if (o.dicNo == "Member_Type") {
            o.data.forEach(i => {
              let obj = {
                label: i.value,
                value: i.key,
                key: i.key
              };
              this.hylxList.push(obj);
            });
          } else if (o.dicNo == "supplier") {
            o.data.forEach(i => {
              let obj = {
                label: i.value,
                value: i.key,
                key: i.key
              };
              this.MerchantLIST.push(obj);
            });
          }
        });
      });
    },

    getFormOption(field) {
      let option;
      this.editFormOptions.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    getFormOptionXQ(field) {
      let option;
      this.editFormOptionsXQ.forEach(x => {
        x.forEach(item => {
          if (item.field == field) {
            option = item;
          }
        });
      });
      return option;
    },
    submit() {
      let card_num = this.editFormFields.card_num;
      let cardno_prefix = this.editFormFields.cardno_prefix;
      let cardno_suffix = this.editFormFields.cardno_suffix;
      let start_no = this.editFormFields.start_no;
      let coupon_count = this.editFormFields.coupon_count;
      let coupon_code = this.editFormFields.coupon_code;
      let coupon_name = this.editFormFields.coupon_name;
      let rule_type = this.editFormFields.rule_type;
      let random_type = this.editFormFields.random_type;
      if (coupon_code == "") {
        this.$Message.error("请输入优惠券模板名称");
        return;
      }
      if (coupon_name == "") {
        this.$Message.error("请输入券名称");
        return;
      }
      if (card_num == "" || card_num < 6 || !/^\d+$/.test(card_num)) {
        this.$Message.error("请输入一个大于等于6的合法数字作为券号位数");
        return;
      }
      if (this.editFormFields.is_generate == 1) {
        if (
          coupon_count == "" ||
          coupon_count < 1 ||
          !/^\d+$/.test(coupon_count)
        ) {
          this.$Message.error("请输入一个大于1的合法数字作为发券数量");
          return;
        }
      } else {
        this.editFormFields.coupon_count = 0
      }

      if (rule_type == 1) {
        if (random_type == "") {
          this.$Message.error("请选择随机模式");
          return;
        }
      } else {
        if (!cardno_prefix == "" || !cardno_suffix == "") {
          console.log(111, cardno_suffix, cardno_prefix);
          console.log(typeof cardno_suffix, "v");
          let calletter = /^[A-Za-z0-9]*$/g;
          if (!calletter.test(cardno_prefix)) {
            this.$Message.error("请输入大小写字母或者数字作为券前缀");
            return;
          }
          let calletter1 = /^[a-zA-z]*$/g;
          if (!calletter1.test(cardno_suffix)) {
            this.$Message.error("请输入大写或者小写字母作为券后缀");
            return;
          }
        } else {
          this.$Message.error("请输入券前后缀");
          return;
        }
        if (start_no == "" || start_no < 1 || /^\D+$/.test(start_no)) {
          this.$Message.error("请输入一个大于一的合法数字作为券起始号");
          return;
        }
      }

      if (this.editFormFields.rule_type == 0) {
        if (card_num < 6 || card_num >= 16) {
          this.$Message.error("券号位数不能超过16位或者不能小于6位");
        } else {
          if (
            card_num >=
            cardno_prefix.length +
              cardno_suffix.length +
              (Number(start_no) + Number(coupon_count)).toString().length
          ) {
            this.interface();
          } else {
            this.$Message.error("自定义模式券号规则位数不能大于券号位数");
          }
        }
      } else {
        this.interface();
      }
    },
    interface() {
      let v = JSON.parse(JSON.stringify(this.editFormFields));
      v.use_starttime = this.editFormFields.cardno_gdrq[0];
      v.use_endtime = this.editFormFields.cardno_gdrq[1];
      delete v.cardno_gdrq;
      if (this.editFormFields.time_type == 0) {
        v.card_day = "";
        if (v.use_starttime == null && v.use_endtime == null) {
          this.$Message.error("请选择有效期规则区间");
          return;
        }
      } else {
        v.use_starttime = null;
        v.use_endtime = null;
        v.card_day = this.editFormFields.card_day;
        // if (v.card_day == '') {
        //   this.$Message.error('请输入发券生效后有效期')
        //   return
        // }
      }
      v.status = 0;
      let param = {
        mainData: v,
        detailData: []
      };
      this.loading = true;
      this.http.post("/api/MebcouponMake/Add", param).then(res => {
        if (res.status) {
          this.model = false;
          this.loading = false;
          this.$message.success("制券成功");
          this.$store.getters.data().viewPageTest.refresh();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    formatLongDate() {
      let date = new Date();
      let myyear = date.getFullYear();
      let mymonth = date.getMonth() + 1;
      let myweekday = date.getDate();
      let myHour = date.getHours();
      let myMin = date.getMinutes();
      let mySec = date.getSeconds();

      if (mymonth < 10) {
        mymonth = "0" + mymonth;
      }
      if (myweekday < 10) {
        myweekday = "0" + myweekday;
      }
      if (myHour < 10) {
        myHour = "0" + myHour;
      }
      if (myMin < 10) {
        myMin = "0" + myMin;
      }
      if (mySec < 10) {
        mySec = "0" + mySec;
      }
      return myyear + mymonth + myweekday + myHour + myMin + mySec;
    }
  }
};
export default vueParam;
</script>
<style>
.sysm > .el-form-item__content {
  width: 87% !important;
}
.mebcouponZQ > .ivu-form .ivu-form-item-label {
  color: #606266;
  font-size: 14px;
}
</style>
<style lang="scss" scoped>
.hx {
  margin: 20px 0;
  border-bottom: 1px solid #ccc;
  padding-bottom: 10px;
}
.z {
  width: 50px;
}
.el-form-item {
  width: 47% !important;
}

.el-transfer {
  margin-top: 20px;
}
.treeS {
  height: 200px;
  overflow-y: scroll;
}
.cascader {
  margin-top: 20px;
  display: flex;
  .casd {
    width: 300px;
    max-height: 300px;
    overflow-y: auto;
  }
  .el-transfer {
    margin-left: 20px;
    margin-top: 0;
  }
}
.sysm {
  width: 100% !important;
  display: flex;
}
.meInt {
  width: 92%;
}
.fr {
  float: right;
}
</style>
